package com.holyn.guofang.presenter.brand;

import com.holyn.guofang.MyApplication;
import com.holyn.guofang.model.sharepreference.UserInfoSharePreference;
import com.holyn.guofang.model.webhttp.RetrofitManager;
import com.holyn.guofang.model.webhttp.observable.BrandObservable;
import com.holyn.guofang.model.webhttp.observable.CardObservable;
import com.holyn.guofang.model.webhttp.observable.UserLoginedObservable;
import com.holyn.guofang.model.webhttp.wo.AlgorismWo;
import com.holyn.guofang.model.webhttp.wo.BrandItemInfoWo;
import com.holyn.guofang.model.webhttp.wo.ImageMergeWo;
import com.holyn.guofang.model.webhttp.wo.ImagePartitionWo;
import com.holyn.guofang.model.webhttp.wo.ImageSimilarWo;
import com.holyn.guofang.model.webhttp.wo.StatusWo;
import com.holyn.guofang.model.webhttp.wo.TaskIDWo;
import com.holyn.guofang.model.webhttp.wo.TaskWo;
import com.holyn.guofang.model.webhttp.wo.UserCardListWo;
import com.holyn.guofang.model.webhttp.wo.UserInfoWo;
import com.holyn.guofang.presenter.BaseView;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * Created by Holyn on 2017/7/21.
 */

public class BrandPresenter extends BrandContract.Presenter{
    private BrandObservable brandObservable;
    private UserLoginedObservable loginedObservable;
    private CardObservable cardObservable;

    private BrandContract.AlgorismWoView algorismWoView;
    private BrandContract.ImgPartitionView imgPartitionView;
    private BrandContract.ImgMergeView imgMergeView;
    private BrandContract.SimilarImgView similarImgView;
    private BrandContract.SearchTradeView searchTradeView;
    private BrandContract.MyDealProcessView myDealProcessView;

    public BrandPresenter(BaseView baseView) {
        super(baseView);
        brandObservable = RetrofitManager.getCommonObservable(BrandObservable.class);
        loginedObservable = RetrofitManager.getCommonObservable(UserLoginedObservable.class);
        cardObservable = RetrofitManager.getCommonObservable(CardObservable.class);
    }

    @Override
    public void addAlgorismWoView(BrandContract.AlgorismWoView algorismWoView) {
        this.algorismWoView = algorismWoView;
    }

    @Override
    public void addImgPartitionView(BrandContract.ImgPartitionView imgPartitionView) {
        this.imgPartitionView = imgPartitionView;
    }

    @Override
    public void addImgMergeView(BrandContract.ImgMergeView imgMergeView) {
        this.imgMergeView = imgMergeView;
    }

    @Override
    public void addSimilarImgView(BrandContract.SimilarImgView similarImgView) {
        this.similarImgView = similarImgView;
    }

    @Override
    public void addSearchTradeView(BrandContract.SearchTradeView searchTradeView) {
        this.searchTradeView = searchTradeView;
    }

    @Override
    public void addMyDealProcessView(BrandContract.MyDealProcessView myDealProcessView) {
        this.myDealProcessView = myDealProcessView;
    }

    @Override
    public void postAlgorism(int requestAction, String typeCode) {
        getCommonObservable(requestAction, brandObservable.algorism(typeCode))
                .subscribe(new Consumer<AlgorismWo>() {
                    @Override
                    public void accept(@NonNull AlgorismWo algorismWo) throws Exception {
                        algorismWoView.responseSuccess(requestAction, algorismWo);
                    }
                });
    }

    @Override
    public void postImgPartition(int requestAction, String SrcImgID) {
        getCommonObservable(requestAction, brandObservable.imgPartition(SrcImgID))
                .subscribe(new Consumer<ImagePartitionWo>() {
                    @Override
                    public void accept(@NonNull ImagePartitionWo imagePartitionWo) throws Exception {
                        imgPartitionView.responseSuccess(requestAction, imagePartitionWo);
                    }
                });
    }

    @Override
    public void postImgMerge(int requestAction, String SrcImgID, String Num) {
        getCommonObservable(requestAction, brandObservable.imgMerge(SrcImgID, Num))
                .subscribe(new Consumer<ImageMergeWo>() {
                    @Override
                    public void accept(@NonNull ImageMergeWo imageMergeWo) throws Exception {
                        imgMergeView.responseSuccess(requestAction, imageMergeWo);
                    }
                });
    }

    @Override
    public void postSimilarImg(int requestAction, String SrcImgID, String ImgIDs, Integer TopCount) {
        getCommonObservable(requestAction, brandObservable.getSimilarImg(SrcImgID, ImgIDs, TopCount))
                .subscribe(new Consumer<ImageSimilarWo>() {
                    @Override
                    public void accept(@NonNull ImageSimilarWo imageSimilarWo) throws Exception {
                        similarImgView.responseSuccess(requestAction, imageSimilarWo);
                    }
                });
    }

    @Override
    public void postTaskID(int requestAction) {
        getCommonObservable(requestAction, brandObservable.getTaskID(UserInfoSharePreference.getUserName(MyApplication.getContext())))
                .subscribe(new Consumer<TaskIDWo>() {
                    @Override
                    public void accept(@NonNull TaskIDWo taskIDWo) throws Exception {
                        searchTradeView.responseSuccess(requestAction, taskIDWo);
                    }
                });
    }

    @Override
    public void postSearchResultByTaskID(int requestAction, Integer TaskID, String SortType, Integer BeginRow, Integer EndRow) {
        getCommonObservable(requestAction, brandObservable.getSearchResultByTaskID(TaskID, SortType, BeginRow, EndRow))
                .subscribe(new Consumer<BrandItemInfoWo>() {
                    @Override
                    public void accept(@NonNull BrandItemInfoWo brandItemInfoWo) throws Exception {
                        if (searchTradeView != null){
                            searchTradeView.responseSuccess(requestAction, brandItemInfoWo);
                        }
                        if (myDealProcessView != null){
                            myDealProcessView.responseSuccess(requestAction, brandItemInfoWo);
                        }
                    }
                });
    }

    @Override
    public void postSearchTradeMarkInfo(int requestAction, Integer TaskID, Integer FrontCount
            , String SearchMode, String SearchType, String Content, String MarkClass, String MarkGroup
            , String QueryMode, String Algorithm, String State, String DateType, String BeginDate, String EndDate) {
        getCommonObservable(requestAction, brandObservable.searchTradeMarkInfo(TaskID, FrontCount
                , SearchMode, SearchType, Content, MarkClass, MarkGroup, QueryMode, Algorithm,  State
                , DateType, BeginDate, EndDate))
                .subscribe(new Consumer<BrandItemInfoWo>() {
                    @Override
                    public void accept(@NonNull BrandItemInfoWo brandItemInfoWo) throws Exception {
                        searchTradeView.responseSuccess(requestAction, brandItemInfoWo);
                    }
                });
    }

    @Override
    public void postTaskList(int requestAction, Integer pageSize, Integer page) {
        getCommonObservable(requestAction, brandObservable.getTaskList(pageSize, page))
                .subscribe(new Consumer<TaskWo>() {
                    @Override
                    public void accept(@NonNull TaskWo taskWo) throws Exception {
                        myDealProcessView.responseSuccess(requestAction, taskWo);
                    }
                });
    }

    @Override
    public void postUserInfo(int requestAction) {
        getCommonObservable(requestAction, loginedObservable.userInfo(""))
                .subscribe(new Consumer<UserInfoWo>() {
                    @Override
                    public void accept(@NonNull UserInfoWo userInfoWo) throws Exception {
                        if (myDealProcessView != null){
                            myDealProcessView.responseSuccess(requestAction, userInfoWo);
                        }
                        if (searchTradeView != null){
                            searchTradeView.responseSuccess(requestAction, userInfoWo);
                        }
                    }
                });
    }

    @Override
    public void postUserCardList(int requestAction, Integer activeState, Integer pageSize, Integer page) {
        getCommonObservable(requestAction, cardObservable.userCardList(activeState, pageSize, page))
                .subscribe(new Consumer<UserCardListWo>() {
                    @Override
                    public void accept(@NonNull UserCardListWo userCardListWo) throws Exception {
                        if (myDealProcessView != null){
                            myDealProcessView.responseSuccess(requestAction, userCardListWo);
                        }
                        if (searchTradeView != null){
                            searchTradeView.responseSuccess(requestAction, userCardListWo);
                        }
                    }
                });
    }

    @Override
    public void postTaskPayCode(int requestAction) {
        getCommonObservable(requestAction, brandObservable.getTaskPayCode(""))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        if (myDealProcessView != null){
                            myDealProcessView.responseSuccess(requestAction, statusWo);
                        }
                        if (searchTradeView != null){
                            searchTradeView.responseSuccess(requestAction, statusWo);
                        }
                    }
                });
    }

    @Override
    public void postTaskPay(int requestAction, Integer taskID, String validCode) {
        getCommonObservable(requestAction, brandObservable.taskPay(taskID, validCode))
                .subscribe(new Consumer<StatusWo>() {
                    @Override
                    public void accept(@NonNull StatusWo statusWo) throws Exception {
                        if (myDealProcessView != null){
                            myDealProcessView.responseSuccess(requestAction, statusWo);
                        }
                        if (searchTradeView != null){
                            searchTradeView.responseSuccess(requestAction, statusWo);
                        }
                    }
                });
    }
}
